package com.ldzg.blockwatcher.config.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadFactory;

/**
 * 异步线程池配置
 *
 * @author jiangxinyi
 * @date 2025/06/30
 */
@Configuration
@EnableAsync
public class AsyncConfig {
    @Bean(name = "asyncTaskExecutor")
    @Primary
    public AsyncTaskExecutor taskScheduler() {
        ThreadFactory threadFactory = Thread.ofVirtual().name("scheduled-virtual-", 1).factory();
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadFactory(threadFactory);
        scheduler.setPoolSize(1000);
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setAwaitTerminationSeconds(60);
        scheduler.initialize();
        return scheduler;
    }
}
